/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.framework.service.interceptor;

import org.jboss.seam.annotations.intercept.AroundInvoke;
import org.jboss.seam.annotations.intercept.Interceptor;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.intercept.InvocationContext;
import org.jboss.seam.transaction.TransactionInterceptor;

import com.miranteinfo.seam.framework.service.TransactionSupportInterceptorImpl;
import com.miranteinfo.seam.framework.service.exception.BusinessException;

/**
 * Intereptor responsavel por limpar a flag de rollback causado por uma {@link BusinessException} do contexto do seam.
 * 
 * @author lucas lins
 * 
 */
@Interceptor(around = TransactionInterceptor.class)
public class BusinessExceptionInterceptorImpl {

	@AroundInvoke
	public Object intercept(InvocationContext invocation) throws Exception {
		Object result = null;
		
		try{
			result = invocation.proceed();
		}finally{
			if(!Contexts.getEventContext().isSet(TransactionSupportInterceptorImpl.TRANSACTION_SUPPORT_STARTED) &&
					Contexts.getEventContext().isSet(TransactionSupportInterceptorImpl.BUSINESS_EXCEPTION_ROLLBACK)){
				Contexts.getEventContext().remove(TransactionSupportInterceptorImpl.BUSINESS_EXCEPTION_ROLLBACK);
			}
		}
		
		return result;
	}
	
}
